<?php
/*
 *订单接口API
 */
namespace Home\Controller;
use Think\Controller;
class OrderController extends Controller {
	public function createOrder(){
		$result=array();
		$json=!empty($_GET['order'])?$_GET['order']:'';
		//$json='{"orderinfo": {"shopid": "1","mobile": "13283718662", "address": "姑姑发现","province": "辽宁","city": "沈阳","county": "和平区", "orderamount": 1699,"consignee": "坎坎坷坷", "deviceid": "866150020037178"},"iteminfo": [{"itemname": "海尔冰柜", "itemid": "575","num": 1,"price": 1699,"productid": "382"}]}';
		$jsondata=json_decode($json,true);
		
		//获取商品信息
		$iteminfo=!empty($jsondata['iteminfo'][0])?$jsondata['iteminfo'][0]:'';
		$orderinfo=!empty($jsondata['orderinfo'])?$jsondata['orderinfo']:'';
		if(!empty($iteminfo))
		{

			//判断此商品是否是货品
			$productid=!empty($iteminfo['productid'])?$iteminfo['productid']:'';
			$itemid=!empty($iteminfo['itemid'])?$iteminfo['itemid']:'';
 			
 			//判断设备是否绑定 商家
 			/*$deviceid=!empty($orderinfo['deviceid'])?$orderinfo['deviceid']:'';
 			$isfen=M('device')->where("devicecode='%s'",$deviceid)->getField('shopid');
			if(empty($isfen)){
				//此设备未绑定商家不能下单
				$result['status']=7;
				$result['msg']='此设备未绑定商家不能下单，不能下单';
				echo json_encode($result,JSON_UNESCAPED_UNICODE);
				return '';
			}*/
			if (!empty($productid))
			{
				//此商品为货品
				
				$productid=$iteminfo['productid'];
				$product=M('product');
				//检测库存
				$inventory=$product->where('productid=%d',$productid)->getField('inventory');
				$inventory=intval($inventory);
				if($iteminfo['num']>$inventory)
				{
					//商品数量大于库存,f返回错误信息
					
					$result['status']=1;
					$result['msg']='商品数量大于库存,返回错误信息';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);
					return '';
					
				}
				//计算订单的总金额
				$productprice=$product->where('productid=%d',$productid)->getField('price');
				$amount=intval($productprice)*intval($iteminfo['num']);
				//检测订单总金额
				if($amount !=$orderinfo['orderamount'])
				{
					
					//订单金额错误，返回错误信息
					$result['status']=0;
					$result['msg']='订单金额错误，返回错误信息';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);
					return '';

				}
				//获取基本信息
				
					$data['mobile']=!empty($orderinfo['mobile'])?$orderinfo['mobile']:'';
					$data['address']=!empty($orderinfo['address'])?$orderinfo['address']:'';
					$data['province']=!empty($orderinfo['province'])?$orderinfo['province']:'';
					$data['city']=!empty($orderinfo['city'])?$orderinfo['city']:'';
					$data['district']=!empty($orderinfo['county'])?$orderinfo['county']:'';
					$data['order_amount']=$amount;

					$data['consignee']=!empty($orderinfo['consignee'])?$orderinfo['consignee']:'';
					$data['deviceid']=!empty($orderinfo['deviceid'])?$orderinfo['deviceid']:'';
					//通过deviceid 获取商家ID
					$data['shopid']=M('device')->where("devicecode='%s'",$data['deviceid'])->getField('shopid');
					//获取商品所属的区域
					$data['areatype']=M('item')->where('itemid=%d',$iteminfo['itemid'])->getField('areatype');
					//订单初始状态为未付款
					$data['order_status']=0;
					$data['create_time']=time();
					$data['order_sn']=getOrderSn();//获取唯一订单号
					//获取此商品的供货商id
					//获取商品信息
					$map['itemname']=$iteminfo['itemname'];
					$map['price']=$productprice;
					$map['number']=$iteminfo['num'];
					$map['itemid']=$iteminfo['itemid'];
					$map['productid']=$iteminfo['productid'];
					$map['specvale']='';
					//获取供货价、商品型号、品牌 、供货商
					$m_temp=M('item')->where('itemid=%d',$iteminfo['itemid'])->getField('itemid,bn,supperid');
					$map['sn']=$m_temp[$iteminfo['itemid']]['bn'];
					$map['supperid']=$m_temp[$iteminfo['itemid']]['supperid'];

					$iteminfo['itemid']=intval($iteminfo['itemid']);
					$map['brand']=M()->query('select brandname from '.C(DB_PREFIX).'brand where brandid=( select brandid from '.C(DB_PREFIX).'item where itemid='.$iteminfo['itemid'] .')');
					$map['brand']=$map['brand'][0]['brandname'];
					//获取供货价
					$map['supperprice']=M('product')->where('productid=%d',$productid)->getField('supprice');
					
					$specvalue=M('product')->where('productid=%d',$productid)->find();
					$map['specvale']=$specvalue['color'].$specvalue['bn'];
				
				//保存订单信息
				$order=M('order');
				$item_order= M('order_item');
				$item=M('item');
				$product=M('product');
				$order->startTrans();
				//var_dump($data);die;
				$orderid=$order->add($data);

				if(!empty($orderid))
				{
					//保存商品基本信息
					
                    $map['orderid']=$orderid;
					$rescid=M('order_item')->add($map);
				}
				//订单生成成功减库存
				if(!empty($orderid))
				{    
					//当为货品时需减 product 表中货 库存和 item表中库存，当仅为item时，只减item表库存
					$p_where['inventory']=array('egt',$map['number']);
					$p_where['productid']=$productid;
					$i_where['inventory']=array('egt',$map['number']);
					$i_where['itemid']=$iteminfo['itemid'];
					$result1=$item->where($i_where)->setDec('inventory',$map['number']);
					$result2=$product->where($p_where)->setDec('inventory',$map['number']);
				}
				if(!empty($orderid)&& !empty($rescid) &&!empty($result2))
				{
					$order->commit();//成功则提交
					$result['status']=4;
					$result['order_sn']=$data['order_sn'];
					$result['time']=time();
					$result['msg']='生成订单成功';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);
					$message=A('Msg');
					$message->Ordernum($orderid);
					return '';

				}else{
					$order->rollback();//失败则回滚
					$result['status']=5;
					$result['msg']='网络错误，请重新下单';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);
					return '';
				}
				//货品end
				
			}
			if(!empty($itemid) && empty($productid))
			{

			
				//此商品为普通商品
				$itemid=$iteminfo['itemid'];
				$item=M('item');
				//检测库存
				$inventory=$item->where('itemid=%d',$itemid)->getField('inventory');
				$inventory=intval($inventory);
				if($iteminfo['num']>$inventory){
					//商品数量大于库存,f返回错误信息
					$result['status']=1;
					$result['msg']='商品数量大于库存,返回错误信息';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);
					
				}
				//计算订单的总金额
				$itemprice=$item->where('itemid=%d',$itemid)->getField('price');
				$amount=intval($itemprice)*intval($iteminfo['num']);
				//检测订单总金额
				if($amount !=$orderinfo['orderamount']){
					
					//订单金额错误，返回错误信息
					$result['status']=0;
					$result['msg']='订单金额错误，返回错误信息';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);

				}
				//获取基本信息
					$data['mobile']=!empty($orderinfo['mobile'])?$orderinfo['mobile']:'';
					$data['address']=!empty($orderinfo['address'])?$orderinfo['address']:'';
					$data['province']=!empty($orderinfo['province'])?$orderinfo['province']:'';
					$data['city']=!empty($orderinfo['city'])?$orderinfo['city']:'';
					$data['district']=!empty($orderinfo['county'])?$orderinfo['county']:'';
					$data['order_amount']=$amount;

					$data['consignee']=!empty($orderinfo['consignee'])?$orderinfo['consignee']:'';
					$data['deviceid']=!empty($orderinfo['deviceid'])?$orderinfo['deviceid']:'';

					//通过deviceid 获取商家ID
					$data['shopid']=M('device')->where("devicecode='%s'",$data['deviceid'])->getField('shopid');
					//获取商品所属的区域
					$data['areatype']=M('item')->where('itemid=%d',$itemid)->getField('areatype');


					//订单初始状态为未付款
					$data['order_status']=0;
					$data['create_time']=time();
					$data['order_sn']=getOrderSn();//获取唯一订单号
					//获取此商品的供货商id
					//获取商品信息
					$map['itemname']=$iteminfo['itemname'];
					$map['price']=$itemprice;
					$map['number']=$iteminfo['num'];
					$map['itemid']=$iteminfo['itemid'];
					$map['specvale']=$item->where('itemid=%d',$itemid)->getField('bn');

					//获取供货价、商品型号、品牌 、供货商
					$m_temp=M('item')->where('itemid=%d',$iteminfo['itemid'])->getField('itemid,bn,supperid');
					$map['sn']=$m_temp[$iteminfo['itemid']]['bn'];
					$map['supperid']=$m_temp[$iteminfo['itemid']]['supperid'];

					$iteminfo['itemid']=intval($iteminfo['itemid']);
					$map['brand']=M()->query('select brandname from '.C(DB_PREFIX).'brand where brandid=( select brandid from '.C(DB_PREFIX).'item where itemid='.$iteminfo['itemid'] .')');
					$map['brand']=$map['brand'][0]['brandname'];
					//获取供货价
					$map['supperprice']=M('item')->where('itemid=%d',$iteminfo['itemid'])->getField('supperprice');
				    
				//保存订单信息
				$order=M('order');
				$item_order= M('order_item');
				$order->startTrans();
				$orderid=$order->add($data);
				$item=M('item');
				if(!empty($orderid))
				{
					//保存商品基本信息
					$map['orderid']=$orderid;

					$rescid=M('order_item')->add($map);
				}
				//订单生成成功减库存
				if(!empty($orderid))
				{
					//当为货品时需减 product 表中货 库存和 item表中库存，当仅为item时，只减item表库存
					$i_where['inventory']=array('egt',$map['number']);
					$i_where['itemid']=$map['itemid'];
					$result1=$item->where($i_where)->setDec('inventory',$map['number']);
				
				}

				if(!empty($orderid)&& !empty($rescid) && !empty($result1) ){
					$order->commit();//成功则提交
					$result['status']=4;
					$result['order_sn']=$data['order_sn'];
					$result['time']=time();
					$result['msg']='生成订单成功';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);

					//生成订单成功,调用短信接口
					$message=A('Msg');
					$message->Ordernum($orderid);
					return '';
				}else{
					$order->rollback();//失败则回滚
					$result['status']=5;
					$result['msg']='网络错误，请重新下单';
					echo json_encode($result,JSON_UNESCAPED_UNICODE);
					return  '';
				}

			}
			//普通商品end
		}
		//参数错误
		$result['status']=6;
		$result['msg']='网络错误，无法接收订单信息';
		echo json_encode($result,JSON_UNESCAPED_UNICODE);
		return '';
		

	}

}